package cn.itcast.bos.action.common;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {

	private static final long serialVersionUID = -2939646740056107833L;
	// 
	protected T model;
	@Override
	public T getModel() {
		return model;
	}
	// 完成模型的实例化
	public BaseAction(){
		Type genericSuperclass = this.getClass().getGenericSuperclass();
		ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
		Class<T> type = (Class<T>) parameterizedType.getActualTypeArguments()[0];
		try {
			model = type.newInstance();
		} catch (InstantiationException | IllegalAccessException e) {
			
			e.printStackTrace();
			System.out.println("模型构造失败。");
		}
		
	}
	
	// 将分页数据压入值栈
	protected int page;
	protected int rows;

	public void setPage(int page) {
		this.page = page;
	}
	public void setRows(int rows) {
		this.rows = rows;
	}
	public void pushPageDateToValueStack(Page<T> pageDate){
		Map<String, Object> map = new HashMap<>();
		map.put("total", pageDate.getTotalElements());
		map.put("rows", pageDate.getContent());
		ActionContext.getContext().getValueStack().push(map);
	}
}
